using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class SelectEquipPanel : MonoBehaviour
{
    private TextMeshProUGUI _equipButtonText;

    public static SelectEquipPanel Instance = null;
    private List<EquipButton> _equipBtns;
    private Transform _equips;

    private bool isInit = false;

    private int _index;

    public void Init()
    {
        if (isInit) return;

        Instance = this;

        transform.Find("EquipButton").GetComponent<Button>().onClick.AddListener(OnEquipBtnClick);
        _equips = transform.Find("Equips");
        _equipButtonText = transform.Find("EquipButton/EquipButtonText").GetComponent<TextMeshProUGUI>();

        //装备
        _equipBtns = new List<EquipButton>();
        for (int i = 0; i < GameManager.Instance._equipDatas.Count; i++)
        {
            GameObject go = GameManager.Instance.LoadPrefab(Constants.PathEquipBtn, true);
            go.transform.SetParent(_equips);
            go.transform.localScale = Vector3.one;
            _equipBtns.Add(go.transform.GetComponent<EquipButton>());
        }

        for (int i = 0; i < _equipBtns.Count; i++)
        {
            _equipBtns[i].Init(i, OnSelectBtnClick);
        }

        for (int i = 0; i < _equipBtns.Count; i++)
        {
            _equipBtns[i].Refresh(i);
        }

        OnSelectBtnClick(GameManager.Instance.WaterGunIndex);
        isInit = true;
    }

    public void OnSelectBtnClick(int index)
    {
        GameManager.Instance.PlayBtnClickAudio();
        EquipData data = GameManager.Instance._equipDatas[index];
        _index = index;

        data = GameManager.Instance._equipDatas[index];

        _equipButtonText.text = data.IsUnlock ? "装备" : $"购买\n￥{data.EquipCost}";

        for (int i = 0; i < _equipBtns.Count; i++)
        {
            _equipBtns[i].SetSelect(index == i);
        }
    }

    public void OnEquipBtnClick()
    {
        GameManager.Instance.PlayBtnClickAudio();
        EquipData data = GameManager.Instance._equipDatas[_index];
        if (data.IsUnlock)
        {
            SelectMissionPanel.Instance.SetPanelActive(true);
            GameManager.Instance.WaterGunIndex = _index;
            GameManager.Instance.SetPanelState(PanelState.LevelSelections);
        }
        else
        {
            GameManager.Instance.BuyEquip(_index,
                () =>
                {
                    _equipButtonText.text = data.IsUnlock ? "装备" : $"购买 {data.EquipCost}";
                    for (int i = 0; i < _equipBtns.Count; i++)
                    {
                        _equipBtns[i].Refresh(i);
                    }
                });
        }
    }

    public void SetPanelActive(bool isActive)
    {
        gameObject.SetActive(isActive);

        for (int i = 0; i < _equipBtns.Count; i++)
        {
            _equipBtns[i].Refresh(i);
        }
    }
}